package com.lingkang.flychat.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.lingkang.flychat.FlyChatApplication;
import com.lingkang.flychat.R;
import com.lingkang.flychat.entity.MessageList;
import com.lingkang.flychat.mapper.gen.DaoSession;
import com.lingkang.flychat.mapper.gen.MessageListDao;
import com.lingkang.flychat.ui.activity.chat.FriendChatActivity;
import com.lingkang.flychat.ui.adapter.MessageListAdapter;

import java.util.Date;
import java.util.List;

public class FragmentMsg extends Fragment {

    private ListView lvMsg;
    private DaoSession daoSession;
    private MessageListDao messageListDao;
    private List<MessageList> messageLists;
    private MessageListAdapter messageListAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return LayoutInflater.from(getActivity()).inflate(R.layout.fragment_home_msg, container, false);
    }

    @Override
    public void onStart() {
        initView();
        initClick();
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        updateView();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (hidden) {

        } else {
            updateView();
        }
        super.onHiddenChanged(hidden);
    }


    private void initClick() {
        lvMsg.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MessageList list = messageListAdapter.getItem(position);
                list.setLastTime(new Date());
                messageListDao.update(list);
                //daoSession.getDatabase().execSQL("update MESSAGE_LIST set LAST_TIME=datetime('now','localtime') where USERNAME='" + list.getUsername() + "'");
                Intent intent = new Intent(getActivity(), FriendChatActivity.class);
                intent.putExtra("username", list.getUsername());
                startActivity(intent);
            }
        });
    }

    private void initView() {
        lvMsg = getActivity().findViewById(R.id.lv_msg);

        // 获取对象
        daoSession = ((FlyChatApplication) getActivity().getApplication()).getDaoSession();
        messageListDao = daoSession.getMessageListDao();
        updateView();
    }

    private void updateView() {
        messageListDao = daoSession.getMessageListDao();
        messageLists = messageListDao.queryRaw("order by LAST_TIME desc");

        messageListAdapter = new MessageListAdapter(getActivity(), messageLists);
        lvMsg.setAdapter(messageListAdapter);
    }

}
